/*
 * This project provides a number of implementations for indexing and evaluating
 * <i>Continuous Top-k Queries over Text Streams</i>
 * and has been developped as part of the work described in the article:
 * 
 * Vouzoukidou, N., Amann, B., & Christophides, V. (2012, October). 
 * Processing continuous text queries featuring non-homogeneous scoring functions.
 * In Proceedings of the 21st ACM international conference on Information and 
 * knowledge management (pp. 1065-1074). ACM.
 * http://dl.acm.org/citation.cfm?id=2398404
 * 
 * This software is distributed under the GNU General Public License and as an
 * additional condition of usage, please cite the aforementioned article.
 * 
 * Copyright (C) 2012 ICS/FORTH, Greece and 
 *               2012 LIP6, France
 * 
 * Author: Vouzoukidou Nelly, email: nelly.vouzoukidou (at) gmail (dot) com
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package gr.forth.ics.continuoustopk;

import gr.forth.ics.continuoustopk.metrics.TermStatistics;
import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * An abstract representation of a set of terms and their corresponding weights.
 * This class is intended to be inherited by {@link Query} and {@link Item}.
 *
 * @author Vouzoukidou Nelly, email: nelly.vouzoukidou (at) gmail (dot) com
 */
abstract public class TermVector implements TermStatistics {
    protected Term[] terms;
    protected double[] weights;

    protected TermVector() {
        terms = null;
        weights = null;
    }

    protected TermVector(Term[] terms, double[] weights) {
        this.terms = terms;
        this.weights = weights;
    }
    
    public TermVector(String[] terms, double[] weights) {
        if (terms.length != weights.length) {
            throw new IllegalArgumentException("Terms and weights arrays must have the same size");
        }
        this.terms = new Term[terms.length];
        for (int i = 0; i < terms.length; i++) {
            this.terms[i] = Term.newTerm(terms[i]);
        }
        this.weights = weights;
    }

    public TermVector(String[] terms, double weight) {
        this.terms = new Term[terms.length];
        for (int i = 0; i < terms.length; i++) {
            this.terms[i] = Term.newTerm(terms[i]);
        }
        weights = new double[terms.length];
        for (int i = 0; i < terms.length; i++) {
            weights[i] = weight;
        }
    }

    /**
     * Returns true if this {@code TermVector} contains the term given as a parameter
     * 
     * @param term the term to be looked up
     * @return true if this {@code TermVector} contains the term given as a parameter; false otherwise
     */
    public boolean contains(Term term) {
        return getWeight(term) != 0;
    }

    private void checkIndex(int i) {
        if (i >= terms.length || i < 0) {
            throw new IllegalArgumentException("Asked for index " + i 
                    + " but the TermVector contains " + terms.length + " terms."
                    + "Accepted values: [0, " + (terms.length - 1) + "].");
        }
    }
    
    /**
     * Returns the i-th term of this {@code TermVector}.
     * 
     * @see termCount()
     * 
     * @param i the index of the term to be returned. Should be in [0, termCount())
     * 
     * @return the i-th term
     * 
     * @throws IllegalArgumentException if i is not within the correct bounds
     */
    public Term getTerm(int i) {
        checkIndex(i);
        return terms[i];
    }

    /**
     * Returns the weight of the i-th term of this {@code TermVector}.
     * 
     * @see termCount()
     * 
     * @param i the index of the term whose weight is to be returned. 
     *          Should be in [0, termCount())
     * @return the i-th term
     * 
     * @throws IllegalArgumentException if i is not within the correct bounds
     */
    public double getWeight(int i) {
        checkIndex(i);
        return weights[i];
    }

    /**
     * Returns the weight of the given term for this {@code TermVector} (or zero 
     * if the term does not exist)
     * 
     * @param term the term whose weight is to be returned
     * 
     * @return the weight of the term given as argument
     */
    @Override
    public double getWeight(Term term) {
        for (int i = 0; i < terms.length; i++) {
            if (terms[i] == term) {
                return weights[i];
            }
        }
        return 0;
    }

    /**
     * Returns the number of terms in this {@code TermVector}.
     * 
     * @return the number of terms in this {@code TermVector}
     */
    public int termCount() {
        return terms.length;
    }

    /**
     * Returns an iterator over the terms of this {@code TermVector}.
     * 
     * @return an iterator over the terms of this {@code TermVector}.
     */
    @Override
    public Iterator<Term> iterator() {
        return new Iterator<Term>() {
            int i = 0;
            @Override
            public boolean hasNext() {
                return i < terms.length;
            }

            @Override
            public Term next() {
                if (!hasNext()) {
                    throw new NoSuchElementException();
                }
                return terms[i++];
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    }

    @Override
    public String toString() {
        return Arrays.toString(terms) + ",weights=" + Arrays.toString(weights);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final TermVector other = (TermVector) obj;
        if (!Arrays.deepEquals(this.terms, other.terms)) {
            return false;
        }
        if (!Arrays.equals(this.weights, other.weights)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 17 * hash + Arrays.deepHashCode(this.terms);
        hash = 29 * hash + Arrays.hashCode(this.weights) + 1;
        return hash;
    }
}
